package com.lxd.kyvideo.config;

import com.lxd.kyvideo.interceptor.CorsInterceptor;
import com.lxd.kyvideo.interceptor.TokenAuthInterceptor;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Value("${server.port:8443}")
    private Integer httpsPort;

    @Value("${server.http.port:8080}")
    private Integer httpPort;


    @Resource
    private CorsInterceptor corsInterceptor;

    @Resource
    private TokenAuthInterceptor tokenAuthInterceptor;

    // windows操作系统图片存储路径
    @Value("${web.windows.upload-path}")
    private String windowsSavePath;

    // linux操作系统图片存储路径
    @Value("${web.linux.upload-path}")
    private String linuxSavePath;

    // 对外访问的资源路径
    @Value("${web.static-access-path}")
    private String staticAccessPath;

    @Resource
    private Environment env;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String osName = env.getProperty("os.name");
        String picPath = "";
        if(osName.contains("Windows")) {
            picPath = windowsSavePath;
        }
        else {
            picPath = linuxSavePath;
        }
        System.out.println("picPath:>>>>>>>>>>>>>>" + picPath);
        System.out.println("staticAccessPath:>>>>>>>>>>>>>>" + staticAccessPath);
        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/resource/2022/02/20/**”开头的，
         * 就给我映射到本机的“D:/uploads/”这个文件夹内，去找你要的资源
         * 注意：D:/uploads/ 后面的 “/”一定要带上
         */
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:/"+picPath);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册跨域拦截器
        registry.addInterceptor(corsInterceptor).addPathPatterns("/api/**");
        // 注册token校验拦截器
        registry.addInterceptor(tokenAuthInterceptor)
                .excludePathPatterns("/api/manager/admin/login")
                .addPathPatterns("/api/manager/**");
    }

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(httpPort);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(httpsPort);
        return connector;
    }
}
